import Vue from 'vue'


// 提供一个不在 UI 上显示的 Vue 组件实例.
// 让其的主要目的是为了保持数据共享和响应式.

const share_data = new Vue({
  data () {
    return {
      shareName: '张三'
    }
  },
  methods: {
    changeName (name) {
      this.shareName = name
    },
    async changeNameAsync (name) {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          this.shareName = name
          resolve(this.shareName)
        }, 2000);
      })
    }
  },
  watch: {
    shareName: {
      handler (newVal, oldVal) {
        if (newVal === oldVal) return
        // otherwise do something.....
      },
      immediate: true
    }
  }
})

export default share_data